package me.caofeng.controller;

import com.sun.org.apache.regexp.internal.RE;
import me.caofeng.annotation.Authentication;
import me.caofeng.enums.CheckStatus;
import me.caofeng.pojo.Carts;
import me.caofeng.pojo.Response;
import me.caofeng.pojo.User;
import me.caofeng.service.api.ICartService;
import me.caofeng.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller
 * @Description: 购物车控制器
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-25 16:42
 * @Version: V1.0
 */
@Controller
@RequestMapping(value = "cart")
@Authentication
public class CartController {
    private static Logger LOGGER = LogManager.getLogger(CartController.class);

    @Autowired
    private ICartService cartService;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Response addItemToCart(
            HttpSession session,
            @RequestParam("id") Integer productId,
            @RequestParam("count") Integer count) {
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.addItemToCart(productId, count, user.getUid());
    }

    /**
     * @Author:曹峰 blog.caofeng.me
     * @Date: 2017/6/26 8:39
     * @Description: 获取购物车详情
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Response<Carts> getCarts(HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.getCarts(user.getUid());
    }

    @RequestMapping(value = "/count/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Carts> updateItemCount(
            @PathVariable("id") Integer cartItemId, @RequestParam("count") Integer count,
            HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.updateItemCount(cartItemId, count, user.getUid());
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Response<Carts> deleteCartItem(@PathVariable("id") Integer id, HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.deleteCartItem(id, user.getUid());
    }

    @RequestMapping(value = "status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Carts> updateCartItemCheckStatus(
            @PathVariable("id") Integer id,
            @RequestParam("status") Integer status,
            HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.updateCartItemCheckStatus(id, CheckStatus.getStatus(status), user.getUid());
    }

    @RequestMapping(value = "status",method = RequestMethod.POST)
    @ResponseBody
    public Response<Carts> updateCartsStatus(@RequestParam("status") Integer status,HttpSession session){
        User user = (User) session.getAttribute(Constant.USER);
        return cartService.updateCartsStatus(user.getUid(),CheckStatus.getStatus(status));
    }


}

